﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Xml;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;
using System.Globalization;

namespace ASUV
{
	public partial class ASQueryResultForm : Form
	{
		#region [members]
		string queryID;
		ProjectFileFoundEventArgs projectDetails;
		#endregion

		#region public ASQueryResultForm(string queryIdentifier, ProjectFileFoundEventArgs args)
		public ASQueryResultForm(string queryIdentifier, ProjectFileFoundEventArgs args)
		{
			InitializeComponent();
			queryID = queryIdentifier;
			projectDetails = args;
			ShowQueryresult(queryIdentifier, args);
		}
		#endregion

		#region private void ShowQueryresult(string queryID, ProjectFileFoundEventArgs projectDetails)
		/// <summary>
		/// Method to populate Analysis Services 2000 Query result to DataGridView
		/// </summary>
		/// <param name="queryID">GUID</param>
		/// <param name="projectDetails">Basic details of project ( project name, location file name etc</param>
		private void ShowQueryresult(string queryID, ProjectFileFoundEventArgs projectDetails)
		{
			try
			{
				XmlDocument xmlDomProjectContent = ProjectFile.GetProjectDetails(projectDetails.ProjectLocation + "\\" + projectDetails.ProjectFileName);
				XmlNode xmlNodeQueryResult = xmlDomProjectContent.SelectSingleNode("ProjectFile/MDXQueries/MDXQuery[ID='" + queryID + "']");
				queryNameTextBox.Text = xmlNodeQueryResult.SelectSingleNode("Name").InnerText;
				string query = xmlNodeQueryResult.SelectSingleNode("QueryAS2K").InnerText.Replace("amp;", "&");
				query = query.Replace("lt;", "<");
				query = query.Replace("gt;", ">");
				query = query.Replace("quote;", "\"");
				queryTextBox.Text = query;
				string result = xmlNodeQueryResult.SelectSingleNode("AS2KResult").InnerText;
				if (result != "<Results>No Result</Results>")
				{
					StringReader stringReaderQueryResult = new StringReader(result);
					DataSet ds = new DataSet();
					ds.Locale = CultureInfo.InvariantCulture;
					ds.ReadXml(stringReaderQueryResult);
					as2kResultDataGridView.DataSource = ds.Tables[0];
				}
				else
				{
					as2kResultDataGridView.Columns.Add("Message", "No Result");
				}
			}
			catch (Exception ex)
			{
				EventLog.WriteEntry("AS Upgrade Verification Tool", ex.ToString(), EventLogEntryType.Error); 
			}
		}
		#endregion

		#region private void ShowQueryresult1(string queryID, ProjectFileFoundEventArgs projectDetails)
		/// <summary>
		/// Method to populate Analysis Services 2005 Query result to DataGridView
		/// </summary>
		/// <param name="queryID">GUID</param>
		/// <param name="projectDetails">Basic details of project ( project name, location file name etc</param>
		private void ShowQueryresult1(string queryID, ProjectFileFoundEventArgs projectDetails)
		{
			try
			{
				XmlDocument xmlDomProjectContent = ProjectFile.GetProjectDetails(projectDetails.ProjectLocation + "\\" + projectDetails.ProjectFileName);
				XmlNode xmlNodeQueryResult = xmlDomProjectContent.SelectSingleNode("ProjectFile/MDXQueries/MDXQuery[ID='" + queryID + "']");
				string result = xmlNodeQueryResult.SelectSingleNode("AS2K5Result").InnerText;
				if (result != "<Results>No Result</Results>")
				{
					StringReader stringReaderQueryResult = new StringReader(result);
					DataSet ds = new DataSet();
					ds.Locale = CultureInfo.InvariantCulture;
					ds.ReadXml(stringReaderQueryResult);
					as2k5ResultDataGridView.DataSource = ds.Tables[0];
				}
				else
				{
					as2k5ResultDataGridView.Columns.Add("Message", "No Result");
				}
			}
			catch (Exception ex)
			{
				EventLog.WriteEntry("AS Upgrade Verification Tool", ex.ToString(), EventLogEntryType.Error); 
			}
		}
		#endregion

		#region private void closeButton_Click(object sender, EventArgs e)
		private void closeButton_Click(object sender, EventArgs e)
		{
			this.Close();
		}
		#endregion

		#region private void ASQueryResultForm_Shown(object sender, EventArgs e)
		private void ASQueryResultForm_Shown(object sender, EventArgs e)
		{
			ShowQueryresult1(queryID,projectDetails);
		}
		#endregion
	}
}